package com.n9.qq.android;

import com.n9.qq.android.firendlist.stTroopNum;
import com.n9.qq.android.login.ShareKey;
import com.n9.qq.android.mock.OfflineListener;

import java.security.PrivateKey;
import java.util.ArrayList;
import java.util.List;

/**
 * q的上下文
 * @author ice
 * @since 2019/2/18 10:48
 */
public class AqContext {
    /**
     * 账号
     */
    private long qqUin;
    private String pw;
    private String tel;


    /**
     * smsToken
     */
    private byte[] smsToken;
    /**
     * 默认key
     */
    private byte[] defaultKey = new byte[16];
    /**
     * sessionKey
     */
    private byte[] sessionKey;
    private byte[] cookie;
    /**
     * 未加密的包头中使用的token
     */
    private byte[] token0143;
    /**
     * reqHead中使用的token
     */
    private byte[] token010a;
    private byte[] stKey;

    private byte[] pwMd5;
    private ShareKey shareKey;
    private byte[] shareKey2;

    public byte[] getShareKey2() {
        return shareKey2;
    }

    public void setShareKey2(byte[] shareKey2) {
        this.shareKey2 = shareKey2;
    }

    private byte[] tgtgtKey;
    private byte[] randomKey;
    private long serverCurTime;

    /**
     * 获取好友消息时需要提交
     */
    private byte[] vCookies;

    private int requestId = 100;

    private int age;
    private String nick;
    /**
     * 性别：01为男，02为女
     */
    private int sex;

    private Status status;

    private List<stTroopNum> vecTroopList = new ArrayList<>();

    /**
     * 掉线监听
     */
    private OfflineListener offlineListener;

    /**
     * 最后一次接收到消息的时间
     */
    private long lastRecvMsgTime = 0l;

    public int getSex() {
        return sex;
    }

    public void setSex(int sex) {
        this.sex = sex;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getNick() {
        return nick;
    }

    public void setNick(String nick) {
        this.nick = nick;
    }

    public long getQqUin() {
        return qqUin;
    }

    public void setQqUin(long qqUin) {
        this.qqUin = qqUin;
    }

    public byte[] getDefaultKey() {
        return defaultKey;
    }

    public void setDefaultKey(byte[] defaultKey) {
        this.defaultKey = defaultKey;
    }

    public byte[] getSessionKey() {
        return sessionKey;
    }

    public void setSessionKey(byte[] sessionKey) {
        this.sessionKey = sessionKey;
    }

    public byte[] getCookie() {
        return cookie;
    }

    public void setCookie(byte[] cookie) {
        this.cookie = cookie;
    }

    public byte[] getToken0143() {
        return token0143;
    }

    public void setToken0143(byte[] token0143) {
        this.token0143 = token0143;
    }

    public byte[] getToken010a() {
        return token010a;
    }

    public void setToken010a(byte[] token010a) {
        this.token010a = token010a;
    }

    public byte[] getStKey() {
        return stKey;
    }

    public void setStKey(byte[] stKey) {
        this.stKey = stKey;
    }

    public ShareKey getShareKey() {
        return shareKey;
    }

    public void setShareKey(ShareKey shareKey) {
        this.shareKey = shareKey;
    }

    public byte[] getTgtgtKey() {
        return tgtgtKey;
    }

    public void setTgtgtKey(byte[] tgtgtKey) {
        this.tgtgtKey = tgtgtKey;
    }

    public byte[] getPwMd5() {
        return pwMd5;
    }

    public void setPwMd5(byte[] pwMd5) {
        this.pwMd5 = pwMd5;
    }

    public byte[] getRandomKey() {
        return randomKey;
    }

    public void setRandomKey(byte[] randomKey) {
        this.randomKey = randomKey;
    }

    public long getServerCurTime() {
        return serverCurTime;
    }

    public void setServerCurTime(long serverCurTime) {
        this.serverCurTime = serverCurTime;
    }

    public byte[] getClientPubKey() {
        return this.getShareKey().getClientPubKey();
    }

    public byte[] getShareTeaKey() {
        return this.getShareKey().getShareTeaKey();
    }

    public String getTel() {
        return tel;
    }

    public void setTel(String tel) {
        this.tel = tel;
    }

    public void setSmsToken(byte[] smsToken) {
        this.smsToken = smsToken;
    }

    public byte[] getSmsToken() {
        return smsToken;
    }

    public int getRequestId() {
        return requestId;
    }
    public int incRequestId() {
        this.requestId++;
        return this.requestId;
    }

    public List<stTroopNum> getVecTroopList() {
        return vecTroopList;
    }

    /**
     * 添加群,防止重复添加
     * @param troopNum
     */
    public void addTroopNum(stTroopNum troopNum) {
        boolean existed = false;
        for (stTroopNum stNum:vecTroopList) {
            if (stNum.GroupUin == troopNum.GroupUin) {
                existed = true;
                break;
            }
        }
        if (!existed) {
            vecTroopList.add(troopNum);
        }
    }

    /**
     * 根据群号获取群名称
     * @param code
     * @return
     */
    public String getTroopNameByCode(long code) {
        return vecTroopList.stream().filter(troop -> troop.GroupCode == code).map(troop -> troop.strGroupName).findFirst().orElse("未知群");
//
//        for (stTroopNum troop : vecTroopList) {
//            if (troop.GroupCode == code) {
//                return troop.strGroupName;
//            }
//        }
//        return "未知群";
    }

    public byte[] getvCookies() {
        return vCookies;
    }

    public void setvCookies(byte[] vCookies) {
        this.vCookies = vCookies;
    }

    public void setPw(String password) {
        this.pw = password;
    }

    public String getPw() {
        return this.pw;
    }

    public Status getStatus() {
        return status;
    }

    public void setStatus(Status status) {
        this.status = status;
    }

    public OfflineListener getOfflineListener() {
        return offlineListener;
    }

    public void setOfflineListener(OfflineListener offlineListener) {
        this.offlineListener = offlineListener;
    }

    public long getLastRecvMsgTime() {
        return lastRecvMsgTime;
    }

    public void setLastRecvMsgTime(long lastRecvMsgTime) {
        this.lastRecvMsgTime = lastRecvMsgTime;
    }
}
